package service

import (
	"teacher-helper/dao"
	"teacher-helper/models"
	"teacher-helper/models/request"
)

func AddOneReward(Reward models.SysReward) error {
	return dao.DB.Create(&Reward).Error
}

func GetRewardList(queryInfo request.QueryInfo) (Rewards []models.SysReward, total int, err error) {
	if queryInfo == (request.QueryInfo{}){
		err = dao.DB.Find(&Rewards).Count(&total).Error
		return
	}
	if queryInfo.Query == "" {
		if err = dao.DB.Model(&models.SysReward{}).Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Find(&Rewards).Error
	} else {
		if err = dao.DB.Model(&models.SysReward{}).Where("name like ?", queryInfo.Query+"%").Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Where("name like ?", queryInfo.Query+"%").Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Find(&Rewards).Error
	}
	return
}

func UpdateReward(Reward models.SysReward) error {
	return dao.DB.Save(&Reward).Error
}

func DeleteReward(id int) error {
	return dao.DB.Where("id = ?", id).Delete(&models.SysReward{}).Error
}


